<?php
namespace Mobstar\Resource;

use Api\Dbal\Doctrine\Resource;
use Api\Application;
use Api\Http\Response;
use Entity\Actor;
use Entity\Film;

/**
 * Class Users
 * @package Mobstar\Resource
 */
class Users extends Resource
{
    protected $entity = 'Entity\User';
    protected $identifier = array('uuid');

    protected $rootElement = 'users';

    public function getXmlSchema() { return "service.xsd"; }
    public function getXmlTemplate() { return "users.xml.php"; }

    /**
     * @param Application $application
     */
    public function __construct(Application $application)
    {
        parent::__construct($application);
    }

    public function custom()
    {
        $results = array();
    }


    public function xsd()
    {
        return;
        echo '<pre>';
        //print_r(apc_fetch('sml'));
        //die;
        $xsd = realpath(__DIR__ . '/../service.xsd');

        $dom = new \DOMDocument('1.0', 'utf-8');
        $dom->preserveWhiteSpace = true;
        $dom->load($xsd);

        $xml = $dom->saveXML();

        $parseObj = str_replace($dom->lastChild->prefix.':', $dom->lastChild->prefix . '.', $xml);

        $ob = simplexml_load_file($xsd);

        // Get namespace
        $ns = array_search("http://www.w3.org/2001/XMLSchema", $ob->getDocNamespaces());
        $ob->registerXPathNamespace($ns,'http://www.w3.org/2001/XMLSchema');

        $classElements = $ob->xpath("//$ns:element");

        print_r($classElements);

        die;
        $result = array();

        foreach ($data as $k => $item)
        {
            if ($k == 'xs.element')
            {
                foreach ($item as $elements)
                {
                    if (isset($elements['@attributes']['name']))
                    {
                        $result[$elements['@attributes']['name']] = array();
                    }
                }
            }
        }

        print_r($result);


        apc_store('sml', $data);
    }

    private function _createEntities()
    {
        $em = $this->em;

        $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
            $em->getConnection()->getSchemaManager()
        );
        $driver->setNamespace('Entity\\');

        $em->getConfiguration()->setMetadataDriverImpl($driver);

        $cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
        $cmf->setEntityManager($em);

        $metadata = $cmf->getAllMetadata();

        try
        {
            $tan = new \Doctrine\ORM\Tools\EntityGenerator();
            $tan->setFieldVisibility('protected');
            $tan->setGenerateStubMethods(true);
            $tan->setRegenerateEntityIfExists(true);
            $tan->setGenerateAnnotations(true);
            $tan->generate($metadata, __DIR__ . '/../doctrine');
        }
        catch (Exception $e)
        {
            print_r($e->getMessage());
            die;
        }
    }
}